package com.fieldchain.ex.fa100.ui.money.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.bean.CoinChoseBean;
import com.fieldchain.ex.fa100.bean.event.CoinEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/13
 */
public class CoinChoseAdapter extends RecyclerView.Adapter<CoinChoseAdapter.ViewHolder> {
    private Context mContext;

    public CoinChoseAdapter(Context mContext) {
        this.mContext = mContext;
    }
    private List<CoinChoseBean> list = new ArrayList<>();

    public void setData(List<CoinChoseBean> list) {
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.item_coinchose,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
         holder.tv.setText(list.get(position).getCoinName().toUpperCase());
         holder.tv.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 EventBus.getDefault().post(new CoinEvent(list.get(position).getCoinName(),1,list.get(position).getId()));
                 ((Activity)mContext).finish();
             }
         });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv;

        public ViewHolder(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv);
        }
    }
}
